package nl.liacs.mms.dn.server;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nl.liacs.mms.dn.entities.DigitalNote;


@SuppressWarnings("serial")
public class UserDigitalNotesServlet extends HttpServlet {
	
	private DigitalNotesService notesService = new DigitalNotesService();
	/**
	 * this function returns an XML with the information of one Digital Note
	 */
	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws IOException {
		
		// set hostname in service for image url
		notesService.setHostname(getHostname(req));
		
		String email = req.getParameter("email");
		List<DigitalNote> notes = notesService.getAllNotesFromUser(email);
		res.setContentType("application/xml");
		res.getWriter().println(notesService.DigitalNotesToXML(notes));
	}
	
	public void doDelete(HttpServletRequest req, HttpServletResponse res) throws IOException{
		String id = req.getParameter("id");
		notesService.deleteNote("",id);
		// set status created if successful
		res.setStatus(200);
		res.getWriter().write("success");
	}
	
	private String getHostname(HttpServletRequest req){
		return "http://" + req.getServerName() + ":" + req.getServerPort();
	}
}
